<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="管理员账号">
              <a-input v-model="queryParam.account" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="管理员姓名">
              <a-input v-model="queryParam.realname" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="状态">
              <a-select v-model="queryParam.status">
                <a-select-option value="">
                  请选择
                </a-select-option>
                <a-select-option :value="1">
                  开启
                </a-select-option>
                <a-select-option :value="0">
                  关闭
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchBtn">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => this.queryParam = { status: '' }">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <router-link :to="{ name: 'AdminAdd' }">
        <a-button type="primary" icon="plus">新建</a-button>
      </router-link>
    </div>

    <a-table
      ref="table"
      :row-key="record=>{return record.adminId}"
      :columns="columns"
      :data-source="data"
      :loading="loading"
      :pagination="pagination"
      @change="onChange"
    >
      <span slot="status" slot-scope="text, record">
        <a-switch
          checked-children="启用"
          un-checked-children="禁用"
          @change="(checked) => {
            changeStatus(record.adminId, checked)
          }"
          :checked="record.adminState == ENABLE ? true : false"
        />
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <router-link :to="{ name: 'AdminEdit', params: { id: record.adminId } }">修改</router-link>
          <a-divider type="vertical" />
          <a @click="deleteOne(record.adminId)">删除</a>
        </template>
      </span>
    </a-table>
  </a-card>
</template>
<script>
import { ENABLE, DISABLE } from '@/utils/config'

const columns = [
  {
    title: 'ID',
    dataIndex: 'adminId'
  },
  {
    title: '账号',
    dataIndex: 'account'
  },
  {
    title: '名字',
    dataIndex: 'realName'
  },
  // {
  //   title: '角色',
  //   dataIndex: 'roleName'
  // },
  {
    title: '状态',
    dataIndex: 'statusText'
    // scopedSlots: { customRender: 'status' }
  },
  // {
  //     title: '创建时间',
  //     dataIndex: 'createTime'
  // },
  {
    title: '操作',
    key: 'action',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'AdminList',
  data () {
    return {
      columns,
      loading: false,
      data: [],
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        pageSizeOptions: ['10', '20', '30', '50', '100'],
        showTotal: (total, range) => {
          return (<div>共 <span>{total}</span> 条</div>)
        },
        onShowSizeChange: (current, pageSize) => {
          this.pageSize = pageSize
        }
      },
      queryParam: {
        status: ''
      },
      roleList: [], // 角色列表
      ENABLE
      // create model
      // visible: false,
      // mdl: {},
      // menuVisible: false,
      // menuMdl: {}
    }
  },
  created () {
    // 获取列表
    this.fetch()
    // 获取角色列表
    // this.getRoleList()
  },
  methods: {
    // 改变每页数量时更新显示
    onChange (pagination) {
      this.pagination.defaultCurrent = pagination.current
      this.pagination.defaultPageSize = pagination.pageSize
      this.fetch()
    },
    // 搜索
    searchBtn () {
      this.pagination.defaultCurrent = 1
      this.fetch()
    },
    // 动态获取列表
    fetch () {
      this.loading = true
      const params = {
        page: this.pagination.defaultCurrent,
        pagesize: this.pagination.defaultPageSize
      }
      this.$common.handleGet('/admin/lists', Object.assign({}, params, this.queryParam)).then(res => {
        this.loading = false
        if (res.code === 200) {
          const result = res.data
          const pagination = { ...this.pagination }
          pagination.total = result.totalInfo.total
          this.data = result.lists
          this.pagination = pagination
        }
      }).catch(() => {
        this.$notification.error({
          message: '错误',
          description: '请求信息失败，请重试'
        })
      })
    },
    // 获取角色列表
    getRoleList () {
      const params = {
        pageNo: 1,
        pageSize: 100
      }
      this.$common.handlePost('/jdsysAdminBody/selectPageList', Object.assign({}, params, this.queryParam)).then(data => {
        if (data.code === 200) {
          const result = data.result
          this.roleList = result.records
        }
      }).catch(() => {
        this.$notification.error({
          message: '错误',
          description: '请求信息失败，请重试'
        })
      })
    },
    // 启用、禁用
    changeStatus (id, status) {
      if (id && status !== '') {
        var statusVal = ENABLE
        var message = ''
        if (status) {
          message = '确定要启用吗？'
        } else {
          message = '确定要禁用吗？'
          statusVal = DISABLE
        }
        const _this = this
        this.$confirm({
          title: message,
          onOk() {
            _this.$common.changeField('/jdsysAdmin/updateByIdStatusAndIsDelete', { id: id, status: statusVal }).then(data => {
              if (data.code === 200) {
                _this.$message.success(data.message)
                _this.fetch()
              } else {
                _this.$message.error(data.message)
              }
            })
            .catch(() => {
              _this.$notification.error({
                message: '错误',
                description: '请求信息失败，请重试'
              })
            })
          },
          onCancel() {
            console.log('Cancel')
          },
          class: 'test'
        })
      } else {
        this.$message.error('参数错误')
      }
    },
    // 删除某一个
    deleteOne(id) {
      this.handleDelete(id)
    },
    handleDelete(deleteId) {
      this.$common
        .deleteOne('/admin/delete/' + deleteId, {}, deleteId)
        .then((res) => {
          if (res) {
            this.fetch()
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    }
  }
}
</script>
